Skip to content

feat: add new HanLP integration with ChineseDocumentSplitter #1943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

julian-risch
Copy link
Member

@julian-risch julian-risch commented Jun 13, 2025

Related Issues

Proposed Changes:

  • This PR introduces a ChineseDocumentSplitter that supports accurate sentence and paragraph splitting for Chinese documents. It leverages the HanLP library for Chinese linguistic analysis, including sentence segmentation and tokenization.
  • It keeps the commit history from drafts created in the haystack repository: feature: Chinese DocumentSplitter haystack#9453 and feat: Add ChineseDocumentSplitter haystack#9494
  • In addition, there is a warm_up method loading the models, support for English language is removed, and ChineseDocumentSplitter is no longer inheriting from the DocumentSplitter
  • Similar to the findings and changes of the following PR, we skip tests for the combination of Windows with python 3.13 because of an incompatibility with the sentence-piece dependency chore: stop testing instructor-embedders on windows + python 3.13 #1941
  • There is a Github workflow running the tests for the integration nightly and at every PR
  • The labeler.yml file has been updated

How did you test it?

We should test with this notebook. It shows how to use the new component in the forked repository: https://github.com/mc112611/haystack/blob/307f8340b2e1a9104efe4e33d8c1885d17143c36/examples/chinese_RAG_test_haystack_chinese.ipynb

Notes for the reviewer

Before this can be reviewed we need to work on:

  • Current implementation of ChineseDocumentSplitter is inheriting from the DocumentSplitter. That's probably not needed.
  • We can remove some parts that are only needed to handle English, for example self.language == english
  • We need to define warm_up in ChineseDocumentSplitter, which should load external data
  • We need to add a usage example to the component docstring
  • py.typed needs to be added
  • Tests. Their should be proper unit tests and only a limited number of integration tests. Similar to NLTK (needs to download extra data) right now all tests are integrations tests. we should change that.

I had a look at the other tokenizers that HanLP supports. All of them seem to be worse than the two tokenizers that we support in this integration. Therefore, I'd limit the user's options to just the two. https://hanlp.hankcs.com/docs/api/hanlp/pretrained/tok.html

Checklist

@github-actions github-actions bot added the type:documentation Improvements or additions to documentation label Jun 13, 2025
@julian-risch julian-risch changed the title feat: add new HanNLP integration with ChineseDocumentSplitter feat: add new HanLP integration with ChineseDocumentSplitter Jun 13, 2025
@julian-risch julian-risch marked this pull request as ready for review June 19, 2025 17:55
@julian-risch julian-risch requested a review from a team as a code owner June 19, 2025 17:55
@julian-risch julian-risch requested review from vblagoje and removed request for a team June 19, 2025 17:55
@julian-risch
Copy link
Member Author

DocumentSplitter keeps a source_id in the meta data. We might want to do the same in ChineseDocumentSplitter https://github.com/deepset-ai/haystack/blob/c18f81283c97b950d14238a9d1fa266c3afaf506/haystack/components/preprocessors/document_splitter.py#L231

docs = result["documents"]
assert all(isinstance(doc, Document) for doc in docs)
assert all(doc.content.strip() != "" for doc in docs)
assert any("。" in doc.content for doc in docs), "Expected at least one chunk containing a full stop."
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We could check more explicitly that chunks end with 。

  2. have a second parameter in the asserts better explaining if the assert fails what the issue is. self documenting for the reader

)
splitter.warm_up()
result = splitter.run(documents=[doc])
docs = result["documents"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check that there are exactly three documents

splitter.warm_up()
result = splitter.run(documents=[doc])
docs = result["documents"]
assert all(doc.content.strip().endswith(("。", "!", "?")) for doc in docs), "Sentence was cut off!"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the test to: if the document contains 。then 。 must be the final character of the document.

split_threshold: int = 0,
respect_sentence_boundary: bool = False,
splitting_function: Optional[Callable] = None,
particle_size: Literal["coarse", "fine"] = "coarse",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could discuss other names such as granularity or segmentation_granularity


return text_splits, split_start_page_numbers, split_start_indices

def _split_by_hanlp_sentence(self, doc: Document) -> List[Document]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also call this when split_by is set to word. therefore let's rename the name of this function


for sentence_idx, sentence in enumerate(sentences):
current_chunk.append(sentence)
if particle_size in {"coarse", "fine"}:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always true

num_words = 0

for sent in reversed(sentences[1:]):
if particle_size in {"coarse", "fine"}:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always true

# 'fine' represents fine granularity word segmentation,
# default is coarse granularity word segmentation

if self.particle_size in {"coarse", "fine"}:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:hanlp topic:CI type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants